package com.wobaby.dao;

import java.sql.Date;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import com.wobaby.util.C3P0Utils;
import com.wobaby.vo.BabyVO;
import com.wobaby.vo.ClassVO;
import com.wobaby.vo.UserGroupVO;
import com.wobaby.vo.UserVO;

public class BabyManagement {
	
	 private Object BabyVO;

	 
	 public void addBaby(BabyVO baby)
	 {
		 int i = KeyIncrementorFactory.getNextInteger("BabyManagement");
		 baby.setBabyID(i);		 
		 String sql = "insert into RN_BABY(BabyID,Name,Sex,Birth)values('"
				+ baby.getBabyID()
				+ "','"
				+ baby.getName()
				+ "','"
				+ baby.getSex()
				+ "','"
				+ baby.getBirth()
				+ "')";
				C3P0Utils.updateQuery(sql);	
		 
	 }
	 
	 
	 
	 public void addBaby1(BabyVO baby)
	 {
		
	 String sql = "insert into RN_BABY(BabyID,Name,Sex,Birth)values('"
				+ baby.getBabyID()
				+ "','"
				+ baby.getName()
				+ "','"
				+ baby.getSex()
				+ "','"
				+ baby.getBirth()
				+ "')";
				C3P0Utils.updateQuery(sql);		 
	 }
	 
	 
	 
	public  void addBaby(BabyVO baby,Map<String,UserVO> elder,ClassVO inClass)
	 {
		
 	 for (String eld :elder.keySet()) {  
				 System.out.println("key= "+ eld + " and value= " + elder.get(eld).getUserID());  
				 String sql =" insert into RN_FAMILY_RELATION(UserID,BabyID,RelationType)values('"
				+ elder.get(eld).getUserID()
				+ "','"
				+ baby.getBabyID()
				+ "','"
				+ eld
				+ "')";
				 C3P0Utils.updateQuery(sql);			 
				  }  		
			 String sql1 =" insert into RN_CLASS_MEMBER(BabyID,ClassID)values('"			
					+baby.getBabyID()
					+ "','"
					+ inClass.getClassID()
					+ "')";
  		    		 C3P0Utils.updateQuery(sql1);			 
					 
}
		 
		
	 public  void modifyBaby(BabyVO baby,Map<String,UserVO> elder,ClassVO inClass)
	 {
		 BabyManagement bm = new BabyManagement();
			UserManagement um = new UserManagement();
	     	ClassManagement cm= new ClassManagement();
		     String sql ="  delete from rn_baby where BabyID = '"+ baby.getBabyID() +"'  ";
	    	 C3P0Utils.updateQuery(sql);
	    	 bm.addBaby1(baby);   //澧炲姞鐨勪负   Baby瀵硅薄     ID涓嶈嚜澧�
		     bm.addBaby(baby,elder,inClass);
		 	 
	 }	 
	 
	 
	 
	 public  void deleteBabies(Collection<BabyVO> babies){
	
		
			 for (BabyVO bv : babies) {
				int BabyID = bv.getBabyID();
				String sql = "delete from rn_baby where BabyID='"
						+ BabyID + "'";
				C3P0Utils.updateQuery(sql);
			
	      }
	 }
	 
	 
	 
	 public  void deleteBaby(BabyVO baby) 
	 {
		String sql = " delete from rn_baby where BabyID = '"+ baby.getBabyID()+ "' ";
		  C3P0Utils.updateQuery(sql);	 
	 }
	 
	 
	 public BabyVO listBabiesByID(Integer BabyID) //鍒楀嚭鎵�湁瀹濆疂
	 {
		 BabyVO bv = null;
         String sql ="select * from rn_baby where BabyID = '" + BabyID + "' ";
		 	ResultSet rs = null;
			rs=C3P0Utils.executeQuery(sql);
			try {
				while(rs.next()){
					Integer babyID = rs.getInt(1);
					String name = rs.getString(2);
					Integer sex  = rs.getInt(3);
					String birth = rs.getString(4);
					bv = new BabyVO(babyID,name,sex,birth);		
					
				}
			}
				 catch (SQLException e) {
						e.printStackTrace();
					}
				 return bv;
	 }
	 
	 
	 public Collection<BabyVO> listAllBabies() //鍒楀嚭鎵�湁瀹濆疂
	 {
		 Collection<BabyVO> list=new ArrayList<BabyVO>();
         String sql ="select * from rn_baby";
		 	ResultSet rs = null;
			rs=C3P0Utils.executeQuery(sql);
			try {
				while(rs.next()){
					Integer babyID = rs.getInt(1);
					String name = rs.getString(2);
					Integer sex  = rs.getInt(3);
					String birth = rs.getString(4);
					BabyVO	bv = new BabyVO(babyID,name,sex,birth);		
					list.add(bv);
				}
			}
				 catch (SQLException e) {
						e.printStackTrace();
					}
				 return list;	 
	 }
	
	 
	 
	 public  Collection<Integer> listBabiesIDByClass(Integer classID)
     {
 	   ArrayList<Integer> bvlist = new ArrayList<Integer>();
		String sql = "select BabyID from rn_class_member where classID='" + classID + "'";
		ResultSet rs = null;
		rs = C3P0Utils.executeQuery(sql);
		try {
			while (rs.next()) {
				int BabyID = rs.getInt(1);
				bvlist.add(BabyID);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bvlist;
 	}
	 
 
	 public Collection<BabyVO> listBabiesByClass(Integer classID)
	 {
         Collection<BabyVO> list=new ArrayList<BabyVO>();   
         Collection<Integer> list1=new ArrayList<Integer>();
         BabyManagement bm= new BabyManagement();  
        list1 = bm.listBabiesIDByClass(classID);
         for(Integer i:list1)
          {
	       list.add(bm.findBabyByID(i));
	      }
				 return list;	
	 }
	  
	 public Collection<Integer> listBabiesIDByElder(Integer userID)
	 {
		 ArrayList<Integer> bvlist = new ArrayList<Integer>();
			String sql = "select BabyID from RN_FAMILY_RELATION where userID='" + userID + "'";
			ResultSet rs = null;
			rs = C3P0Utils.executeQuery(sql);
			try {
				while (rs.next()) {
					int BabyID = rs.getInt(1);
					bvlist.add(BabyID);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return bvlist;
	 }
	 
	 public Collection<BabyVO> listBabiesByElder(Integer userID)
	 {
         Collection<BabyVO> list=new ArrayList<BabyVO>();   
         Collection<Integer> list1=new ArrayList<Integer>();
         BabyManagement bm= new BabyManagement();  
        list1 = bm.listBabiesIDByElder(userID);
       for(Integer i:list1)
         {
		   list.add(bm.findBabyByID(i));
		   }
	 return list;	
	 }
	  
	 
	 
	 public Collection<Integer> findElderIDByBaby(BabyVO baby)
	 {
		
		 ArrayList<Integer> list = new ArrayList();
	String sql ="   select UserID  from rn_family_relation where BabyID = '"+ baby.getBabyID()+ "'";
	ResultSet rs = null;
	rs = C3P0Utils.executeQuery(sql);
	try {
		while (rs.next()) {
			int UserID = rs.getInt(1);
			list.add(UserID);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}

	return list;
	 }
	 
	 
	 public Collection<UserVO> findElderByBaby(BabyVO baby)
	 {
         Collection<UserVO> list=new ArrayList<UserVO>();   
         Collection<Integer> list1=new ArrayList<Integer>();
         BabyManagement bm= new BabyManagement();  
         UserManagement um= new UserManagement();
         list1 = bm.findElderIDByBaby(baby);
        System.out.print(bm.findElderIDByBaby(baby));
  
        for(Integer i :list1)
      {
    	 list.add(um.lookupUserByID(i));
      }
		return list;
	
	 }
	  
	 
	 
	 
	 
	 
	 public BabyVO findBabyByID(Integer babyID)
	 { 
		 BabyVO bv = null;
		 String sql ="select * from rn_baby where BabyID = '"+  babyID +"' ";
		 	ResultSet rs = null;
			rs=C3P0Utils.executeQuery(sql);
			try {
				while(rs.next()){
			    	//Integer babyID = rs.getInt(1);
					String name = rs.getString(2);
					Integer sex  = rs.getInt(3);
					String birth = rs.getString(4);
					bv = new BabyVO(babyID,name,sex,birth);				
				}
			}
				 catch (SQLException e) {
						e.printStackTrace();
					}
				 return bv;
	 
	 }
	
}
